<template>
  <div class="app-container">
    <!--    <el-form :inline="true" :model="searchFrom" class="demo-form-inline" size="small">-->
    <!--      <el-form-item>-->
    <!--        <el-input v-model="searchFrom.number" placeholder="分类编号"/>-->
    <!--      </el-form-item>-->
    <!--      <el-form-item>-->
    <!--        <el-input v-model="searchFrom.name" placeholder="分类名称"/>-->
    <!--      </el-form-item>-->
    <!--      <el-form-item>-->
    <!--        <el-button type="primary" @click="onSearch">查询</el-button>-->
    <!--      </el-form-item>-->
    <!--    </el-form>-->
    <el-row class="tool">
      <el-col :span="24">
        <el-button type="primary" size="mini" @click="create()">新增主题</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="编号" width="80">
        <template slot-scope="scope">
          {{ scope.row.seasonsId }}
        </template>
      </el-table-column>
      <el-table-column label="图片" width="110" align="center">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.seasonsPicture"
            style="width: 60px; height: 60px"
            :src="scope.row.seasonsPicture"
            :preview-src-list="[scope.row.seasonsPicture]"
          />
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="主题名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.seasonsName }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="最新更新时间">
        <template slot-scope="scope">
          <i class="el-icon-time"/>
          <span>{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="基本操作" width="200">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="edit(scope.row)">修改</el-button>
          <el-button type="danger" size="mini" @click="deleteCategory(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="app-pagination">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        @prev-click="handlePrevChange"
        @next-click="handleNextChange"
      />
    </div>
  </div>
</template>

<script>
import { deleteCategory, deleteSeasonsCategory, getSeasonsCategoryPage } from '@/api/Category'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        0: 'danger'
      }
      return statusMap[status]
    },
    statusSex(status) {
      const statusMap = {
        1: '女',
        0: '男'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      searchFrom: {
        name: '',
        number: '',
        email: '',
        type: 1
      },
      list: null,
      listLoading: true,
      total: 0,
      limit: 10,
      page: 1
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const searchParams = this.searchFrom
      searchParams['page'] = this.page
      searchParams['limit'] = this.limit
      getSeasonsCategoryPage(searchParams).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    onSearch() {
      this.fetchData()
    },
    /* 上一页*/
    handlePrevChange(event) {
      this.page = event
      this.fetchData()
    },
    /* 下一页*/
    handleNextChange(event) {
      this.page = event
      this.fetchData()
    },
    /* 数字点击*/
    handleCurrentChange(event) {
      this.page = event
      this.fetchData()
    },
    handleSizeChange(event) {
      this.limit = event
      this.fetchData()
    },
    edit(event) {
      this.$router.push({
        path: '/category/seasons/create',
        query: {
          seasonsId: event.seasonsId
        }
      })
    },
    deleteCategory(event) {
      this.$confirm('此操作将分类该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteSeasonsCategory({ ids: event.seasonsId }).then(res => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          } else {
            this.$message.error('网络错误')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    create() {
      this.$router.push('/category/seasons/create')
    }
  }
}
</script>
